package com.link2room.soundwave.service.impl;

import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.TryExceptionUtil;
import com.link2room.jetfire.constant.ReservationConstant;
import com.link2room.jetfire.dto.result.base.ReservationBaseDto;
import com.link2room.soundwave.entity.L2RYhHotel;
import com.link2room.soundwave.entity.L2RYhReservation;
import com.link2room.soundwave.entity.L2RYhReservationDetail;
import com.link2room.soundwave.service.IYhReservationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yuheng.pms.result.YhOrderReturnDto;

import javax.annotation.Resource;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author sqj88
 * @date 2017/12/15 17:07
 */
@Service("yhReservationService")
public class YhReservationServiceImpl implements IYhReservationService {
    @Resource
    private ICommonService commonService;

    @Override
    @Transactional
    public void saveYhReservationAll(ReservationBaseDto reservationBaseDto, L2RYhHotel l2RYhHotel, YhOrderReturnDto yhOrderReturnDto)throws Exception{
        commonService.saveList(yhOrderReturnDto.getSerialNoList().stream().map(value->{
            L2RYhReservationDetail l2RYhReservationDetail=new L2RYhReservationDetail();
            l2RYhReservationDetail.setLhotelGroupCode(reservationBaseDto.getLhotelGroupCode() );
            l2RYhReservationDetail.setLhotelCode( reservationBaseDto.getLhotelCode());
            l2RYhReservationDetail.setL2RNo( reservationBaseDto.getL2RNo());
            l2RYhReservationDetail.setHotelCode(l2RYhHotel.getHotelCode());
            l2RYhReservationDetail.setGroupCode(l2RYhHotel.getGroupCode() );
            l2RYhReservationDetail.setCrsNo(yhOrderReturnDto.getParentReserveNo());
            l2RYhReservationDetail.setStatus(ReservationConstant.YH_RESSTA.R);
            l2RYhReservationDetail.setHstatus(ReservationConstant.YH_RESSTA.R);
            l2RYhReservationDetail.setSerialNo(value);
            return l2RYhReservationDetail;
        }).collect(Collectors.toList()));

        L2RYhReservation l2RYhReservation = new L2RYhReservation();
        l2RYhReservation.setLhotelGroupCode(reservationBaseDto.getLhotelGroupCode() );
        l2RYhReservation.setLhotelCode( reservationBaseDto.getLhotelCode());
        l2RYhReservation.setL2RNo( reservationBaseDto.getL2RNo());
        l2RYhReservation.setHotelCode(l2RYhHotel.getHotelCode());
        l2RYhReservation.setGroupCode(l2RYhHotel.getGroupCode() );
        l2RYhReservation.setCrsNo(yhOrderReturnDto.getParentReserveNo());
        l2RYhReservation.setPmsSta(ReservationConstant.YH_RESSTA.R);
        l2RYhReservation.setOpenId(reservationBaseDto.getOpenid());
        l2RYhReservation.setRsvMan(reservationBaseDto.getRsvMan());
        l2RYhReservation.setMemberNo(reservationBaseDto.getMemberNo());
        commonService.save(l2RYhReservation );
    }

    @Override
    public void updateYhReservationAll(L2RYhReservation l2RYhReservation, String pmsSta) throws Exception {
        L2RYhReservationDetail queryYhReservationDetail=new L2RYhReservationDetail();
        queryYhReservationDetail.setLhotelGroupCode(l2RYhReservation.getLhotelGroupCode());
        queryYhReservationDetail.setLhotelCode(l2RYhReservation.getLhotelCode());
        queryYhReservationDetail.setCrsNo(l2RYhReservation.getCrsNo());
        QueryResult<L2RYhReservationDetail> l2RYhReservationDetailResult =commonService.findAllEQ(L2RYhReservationDetail.class,queryYhReservationDetail);
        Optional.ofNullable(l2RYhReservationDetailResult.getResultlist()).filter(list->list.size()>0).ifPresent(
            TryExceptionUtil.of(value->{
                value.stream().forEach(event->event.setStatus(pmsSta));
                commonService.mergeList(value);
            })
        );

        l2RYhReservation.setPmsSta(pmsSta);
        commonService.merge(l2RYhReservation);
    }
}
